﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;


namespace SCSService
{
    public class PostArticleService
    {
        //Thêm tin vào Post_in_Category
        public int CreatePost_In_Category(Post_In_Category PostInCategory)
        { 
            using(var data=new DataDataContext())
            {
                data.Post_In_Categories.InsertOnSubmit(PostInCategory);
                data.SubmitChanges();
                return PostInCategory.PostID;
            }
        }
        //Xóa hết các categoryID trong Post_In_Category theo PostId
        public void DeletePost_In_CategoryByPostID(int? iPostId)
        { 
            using(var data= new DataDataContext())
            {
                var list = (from p in data.Post_In_Categories
                            where p.PostID == iPostId
                            select p).ToList();
                data.Post_In_Categories.DeleteAllOnSubmit(list);
                data.SubmitChanges();
            }
        }
        public List<int> GetCategoryIDByPostID(int? iPostId)
        { 
            using(var data=new DataDataContext())
            {
                var type=(from cate in data.Post_In_Categories
                            where cate.PostID==iPostId
                            select (int)cate.CategoryID).ToList();
                return type;
            }
        }
        //Lấy tất danh mục bài viết
        public List<Post_Category> GetAllCatePostArticle(int iLanguage,int iSiteGuid)
        { 
            using(var data=new DataDataContext())
            {
                var type = (from cate in data.Post_Categories
                            where cate.LanguagesID == iLanguage && cate.SiteGuid==iSiteGuid
                            select cate).DefaultIfEmpty();
                return type.ToList();
            }
        }
        //update post article
        public int UpdatePostArticle(Post PostArticle)
        {
            using(var data =new DataDataContext())
            {
                var type = (from p in data.Posts
                            where p.PostID == PostArticle.PostID
                            select p).FirstOrDefault();
               
                type.PostTitle = PostArticle.PostTitle;
                type.PostContent = PostArticle.PostContent;
                type.PostAbstract = PostArticle.PostAbstract;
                type.PostCreat = PostArticle.PostCreat;
                type.ModifyDate = PostArticle.ModifyDate;
                type.MetaDesciption = PostArticle.MetaDesciption;
                type.PostImage = PostArticle.PostImage;
                type.PostSource = PostArticle.PostSource;
                type.Title = PostArticle.Title;
                data.SubmitChanges();
                return PostArticle.PostID;
            }

        }
        //Get post
        public Post GetPostByPostId(int? PostId)
        { 
            using(var data=new DataDataContext())
            {
                var type = (from p in data.Posts
                            where p.PostID == PostId
                            select p).FirstOrDefault();
                return type;
            }
        }
        //xóa bài viết
        public void DeletePost(int? PostId)
        { 
            using(var data =new DataDataContext())
            {
                var post = (from p in data.Posts
                            where p.PostID == PostId
                            select p).FirstOrDefault();
                data.Posts.DeleteOnSubmit(post);
                data.SubmitChanges();
                
            }
        }
        //Join bang: Post,Post_In_Category,Post_Category.
        public List<Post> GetListPostByIdCate(int? cate,int? iPostUser,int iSiteGuid,int iLanguageId)
        {
            using(var datas=new DataDataContext())
            {
                var data = (from p in datas.Posts
                            join pic in datas.Post_In_Categories on p.PostID equals pic.PostID into p_pic
                            from r0 in p_pic.DefaultIfEmpty()
                            join pc in datas.Post_Categories on r0.CategoryID equals pc.CategoryID into post_cate
                            from r1 in post_cate
                            where r1.SiteGuid == iSiteGuid &&  r1.LanguagesID==iLanguageId
                            select new 
                            {
                             p,
                             post_cate
                            }).DefaultIfEmpty();

              if (cate!=null)
              {
                  data=data.Where(m => m.p.PostID == cate);  
              }
              if (iPostUser != null)
              {
                  data = data.Where(m=>m.p.PostUser==iPostUser);
              }
              var Result = (from ps in data.ToList()
                            select new Post
                            {  
                             PostID=ps.p.PostID,
                             PostStatus=ps.p.PostStatus,
                             Title=ps.p.Title,
                             PostUser=ps.p.PostUser,
                             PostContent=ps.p.PostContent,
                             PostSource=ps.p.PostSource,
                             PostLink=ps.p.PostLink,
                             PostImage=ps.p.PostImage,
                             PostCreat=ps.p.PostCreat,
                             ModifyDate=ps.p.ModifyDate,
                             AttachFile=ps.p.AttachFile,
                             MetaDesciption=ps.p.MetaDesciption,
                             PostAbstract=ps.p.PostAbstract,
                             PostTitle=ps.p.PostTitle,
                             Post_In_Categories=ps.p.Post_In_Categories
                             
                            }).DefaultIfEmpty();
               return Result.ToList();
            }
        }
        //Update trạng thái bài post
        public void UpdateStatus(int? iId,int? iPostStatus)
        {
            using(var data =new DataDataContext())
            {
                var post=(from p in data.Posts
                              where p.PostID==iId
                              select p).FirstOrDefault();

                post.PostStatus = (byte)iPostStatus;
                data.SubmitChanges();
            }
        }
    }
}
